package com.study.spring.ch03.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/ch03/reqParamBind/**") // ①处理器的通用映射前缀
public class ReqParamBindController {

	/**
	 * 表示请求参数中有“create01”的参数名且请求方法为“GET”即可匹配
	 * http://127.0.0.1:8080/spring//ch03/reqParamBind/test?username=123&create01=1
	 * 
	 * @return
	 */
	@RequestMapping(params = "create01", method = RequestMethod.GET)
	public String reqParamBind01(@RequestParam String username) {
		System.out.println("================reqParamBind01:" + username);
		return "ch03/create";
	}

	/**
	 * 表示请求参数中有“create02”的参数名且请求方法为“GET”即可匹配
	 * http://127.0.0.1:8080/spring//ch03/reqParamBind/test?username=123&create02=1
	 * 
	 * @return
	 */
	@RequestMapping(params = "create02", method = RequestMethod.GET)
	public String reqParamBind02(@RequestParam(value = "username", required = false) String username) {
		System.out.println("================reqParamBind02:" + username);
		return "ch03/create";
	}

	/**
	 * http://127.0.0.1:8080/spring//ch03/reqParamBind/create03?role=admin&role=user
	 * 
	 * @return
	 */
	@RequestMapping(value = "create03", method = RequestMethod.GET)
	public String reqParamBindForMultiValue01(@RequestParam(value = "role") String[] roleList) {
		System.out.println("================reqParamBind02:" + roleList);
		return "ch03/create";
	}

	/**
	 * http://127.0.0.1:8080/spring//ch03/reqParamBind/create04?role=admin&role=user
	 * 
	 * @return
	 */
	@RequestMapping(value = "create04", method = RequestMethod.GET)
	public String reqParamBindForMultiValue02(@RequestParam(value = "role") List<String> list,
			@CookieValue(value = "JSESSIONID", defaultValue = "") Cookie sessionId) {
		System.out.println("================reqParamBind02:" + list);
		return "ch03/create";
	}

	/**
	 * http://127.0.0.1:8080/spring//ch03/reqParamBind/pathValueTest/users/123/topics/456
	 * 
	 * @return
	 */
	@RequestMapping(value = "/users/{userId}/topics/{topicId}")
	public String reqParamBindForPathValue(@PathVariable(value = "userId") int userId,
			@PathVariable(value = "topicId") int topicId,
			@CookieValue(value = "JSESSIONID", defaultValue = "") String sessionId,
			@RequestHeader("User-Agent") String userAgent, @RequestHeader(value = "Accept") String[] accepts) {
		System.out.println("================pathValueTest:" + userId + ";" + topicId);
		return "ch03/create";
	}

	/**
	 * http://localhost:8080/spring/ch03/reqParamBind/users/42;q=11;r=12/others/21;q=22;s=23
	 * 如果只有一个地方有q，也可以这么取，但如果有多个q，这样就错了，必须像第二个那样去指定取谁的q值
	 * 
	 * @MatrixVariable int q,
	 * @param q1
	 * @param q2
	 * @param matrixVars
	 * @param userIdMatrixVars
	 */
	@RequestMapping(value = "/users/{userId}/others/{otherUserId}", method = RequestMethod.GET)
	public String hello(@MatrixVariable(value = "q", pathVar = "userId") int q1,
			@MatrixVariable(value = "q", pathVar = "otherUserId") int q2,
			@MatrixVariable Map<String, String> matrixVars,
			@MatrixVariable(pathVar = "userId") Map<String, String> userIdMatrixVars) {
		// System.out.println("q=="+q);
		System.out.println(
				"q1=" + q1 + ",q2=" + q2 + ",matrixVars=" + matrixVars + ",userIdMatrixVars=" + userIdMatrixVars);
		
		return "ch03/success";
	}

}
